package com.example.weixin;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import java.nio.ByteBuffer;
import java.util.List;

import static android.os.SystemClock.sleep;

public class TestAccessibility extends AccessibilityService {
    private static TestAccessibility mService;
    private static Handler mHandler = new Handler();
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        mService = this;
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
   //     PrintUtils.printEvent(event);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        findAndPerformActionByID("com.tencent.mm:id/gdh");
        findAndPerformActionByIDAdd("com.tencent.mm:id/h8v");
        findAndPerformActionByIDLink("com.tencent.mm:id/fg1");
        findAndPerformActionByIDAddress("com.tencent.mm:id/dal");
        findAndPerformActionByIDSend("com.tencent.mm:id/d4y");
        findAndPerformActionByIDBack("com.tencent.mm:id/bd3");
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void findAndPerformActionByID(String ID) {//更多
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.ImageView")) {
                    Click();
                }
            }
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void findAndPerformActionByIDAdd(String ID) {//添加朋友
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.LinearLayout")) {
                    ClickAdd();
                }
            }
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void findAndPerformActionByIDLink(String ID) {//手机联系人
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.LinearLayout")) {
                    dispatchGestureView(450,1220);
                }
            }
        }
    }
    private void Click() {
        //点击更多
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            final Path path = new Path();
            path.moveTo(1006, 163);   //X和Y是需要双击的按钮坐标
            GestureDescription.Builder builder = new GestureDescription.Builder();
            GestureDescription gestureDescription = null;
            gestureDescription = builder.addStroke(new GestureDescription.StrokeDescription(path, 30, 10)).build();
            dispatchGesture(gestureDescription, new GestureResultCallback() {
                @Override
                public void onCompleted(GestureDescription gestureDescription) {
                    super.onCompleted(gestureDescription);
                }
                @Override
                public void onCancelled(GestureDescription gestureDescription) {
                    super.onCancelled(gestureDescription);
   //                                 Log.d("------", "更多");
                }
            }, null);
        }
    }
    private void ClickAdd() {
        //点击添加朋友
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            final Path path = new Path();
            path.moveTo(810, 475);   //X和Y是需要双击的按钮坐标
            GestureDescription.Builder builder = new GestureDescription.Builder();
            GestureDescription gestureDescription = null;
            gestureDescription = builder.addStroke(new GestureDescription.StrokeDescription(path, 30, 10)).build();
            dispatchGesture(gestureDescription, new GestureResultCallback() {
                @Override
                public void onCompleted(GestureDescription gestureDescription) {
                    super.onCompleted(gestureDescription);
                }
                @Override
                public void onCancelled(GestureDescription gestureDescription) {
                    super.onCancelled(gestureDescription);
  //                  Log.d("------", "添加朋友");
                }
            }, null);
        }
    }
    private void ClickBack() {
        //点击手机联系人
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            final Path path = new Path();
            path.moveTo(50, 170);   //X和Y是需要双击的按钮坐标
            GestureDescription.Builder builder = new GestureDescription.Builder();
            GestureDescription gestureDescription = null;
            gestureDescription = builder.addStroke(new GestureDescription.StrokeDescription(path, 30, 10)).build();
            dispatchGesture(gestureDescription, new GestureResultCallback() {
                @Override
                public void onCompleted(GestureDescription gestureDescription) {
                    super.onCompleted(gestureDescription);
                }
                @Override
                public void onCancelled(GestureDescription gestureDescription) {
                    super.onCancelled(gestureDescription);
                   // Log.d("------", "手机联系人");
                }
            }, null);
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void dispatchGestureView(int x, int y) {
        Log.e("Tag", "模拟点击事件");
        Point position = new Point(x, y);
        GestureDescription.Builder builder = new GestureDescription.Builder();
        Path p = new Path();
        p.moveTo(position.x, position.y);
        builder.addStroke(new GestureDescription.StrokeDescription(p, 0L, 500L));
        GestureDescription gesture = builder.build();
        dispatchGesture(gesture, new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
//                Log.e("Tag", "onCompleted: 完成..........");
            }
            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
                //Log.e("Tag", "onCompleted: 取消..........");
            }
        }, null);
    }
    private void MyGesture() {//仿滑动
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            Path path = new Path();
            path.moveTo(500, 2000);//设置Path的起点
            path.lineTo(500, 10);
            GestureDescription.Builder builder = new GestureDescription.Builder();
            GestureDescription description = builder.addStroke(
                    new GestureDescription.StrokeDescription(path, 10L, 5L)).build();
            boolean  ret=mService.dispatchGesture(description,
                    new AccessibilityService.GestureResultCallback() {
                        @Override
                        public void onCompleted(GestureDescription gestureDescription) {
                            super.onCompleted(gestureDescription);
                            //                        Log.d(TAG,"滑动成功");
                        }
                        @Override
                        public void onCancelled(GestureDescription gestureDescription) {
                            super.onCancelled(gestureDescription);
                            //                        Log.d(TAG,"滑动失败 ");
                        }
                    }, null);
        }
    }

    private void findAndPerformActionByIDAddress(String ID) {//通讯录添加
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.FrameLayout")) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            } else{
                MyGesture();
            }
        }
    }
    private void findAndPerformActionByIDSend(String ID) {//发送
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.Button")) {
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }
    private void findAndPerformActionByIDBack(String ID) {//返回
        if (getRootInActiveWindow() == null)//取得当前激活窗体的根节点
            return;
        //通过ID找到当前的节点
        @SuppressLint({"NewApi", "LocalSuppress"}) List nodes = getRootInActiveWindow().findAccessibilityNodeInfosByViewId(ID);
        for (int i = 0; i < nodes.size(); i++) {
            AccessibilityNodeInfo node = (AccessibilityNodeInfo) nodes.get(i);
            // 执行点击行为
            if (node != null && node.isEnabled()) {
                if (node.getClassName().equals("android.widget.RelativeLayout")) {
                //    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    ClickBack();
                }
            }
        }
    }
    @Override
    public void onInterrupt() {
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
